package com.sinozo.data.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zhaoyb
 * @version 1.0
 * @description
 * @date 2023/5/31 14:23:45
 */
public class ValidateUtils {


    public static Boolean validate(JSONObject jsonObject) {
        if (jsonObject == null){
            return false;
        }
        //校验common数据
        if (!validateCommon(jsonObject)) {
            return false;
        }
        //校验pages页面数据
        if (!validatePages(jsonObject)) {
            return false;
        }
        //校验actions页面数据
        if (!validateActions(jsonObject)) {
            return false;
        }

        //校验start启动数据
        if (!validateStart(jsonObject)) {
            return false;
        }
        //校验在线日志数据
        if (!validateOnline(jsonObject)) {
            return false;
        }
        return true;
    }

    private static boolean validatePages(JSONObject jsonObject) {
        JSONArray pages = jsonObject.getJSONArray("pages");
        if (pages == null || pages.isEmpty()) {
            return true;
        }
        boolean flag = true;
        for (int i = 0; i < pages.size(); i++) {
            JSONObject page = pages.getJSONObject(i);
            Long  duringTime = page.getLong("during_time");
            if (duringTime == null || duringTime < 1000L) {
                flag = false;
                break;
            }
            String event = page.getString("event");
            if (StringUtils.isEmpty(event)){
                flag = false;
                break;
            }
            String subEvent = page.getString("sub_event");
            if (StringUtils.isEmpty(subEvent)){
                flag = false;
                break;
            }
        }
        return flag;
    }

    private static boolean validateActions(JSONObject jsonObject) {
        JSONArray actions = jsonObject.getJSONArray("actions");
        if (actions == null || actions.isEmpty()) {
            return true;
        }
        boolean flag = true;
        for (int i = 0; i < actions.size(); i++) {
            JSONObject action = actions.getJSONObject(i);
            String actionType = action.getString("action_type");
            if (StringUtils.isEmpty(actionType)){
                flag = false;
                break;
            }
            String event = action.getString("event");
            if (StringUtils.isEmpty(event)){
                flag = false;
                break;
            }
            String subEvent = action.getString("sub_event");
            if (StringUtils.isEmpty(subEvent)){
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static boolean validateOnline(JSONObject jsonObject) {
        //获取启动信息
        String online = jsonObject.getString("online");
        if (StringUtils.isEmpty(online)) {
            return true;
        }
        JSONObject onlineObj = JSON.parseObject(online);
        Long onlineTime = onlineObj.getLong("online_time");
        if (onlineTime == null) {
            return false;
        }
        return true;

    }

    public static Boolean validateStart(JSONObject jsonObject) {
        //获取启动信息
        String start = jsonObject.getString("start");
        if (StringUtils.isEmpty(start)) {
            return true;
        }
        JSONObject startObj = JSON.parseObject(start);
        Integer startWay = startObj.getInteger("start_way");
        if (startWay == null) {
            return false;
        }
        return true;
    }

    public static Boolean validateCommon(JSONObject jsonObject) {
        String common = jsonObject.getString("common");
        //这个是必填数据
        if (StringUtils.isEmpty(common)) {
            return false;
        }
        //校验用户设备号
        JSONObject commonObj = JSON.parseObject(common);
        String deviceId = commonObj.getString("device_id");
        if (StringUtils.isEmpty(deviceId)) {
            return false;
        }
        //校验渠道号
        String qid = commonObj.getString("qid");
        if (StringUtils.isEmpty(qid)) {
            return false;
        }
        //校验渠道分组
        String groupQid = commonObj.getString("group_qid");
        if (StringUtils.isEmpty(groupQid)) {
            return false;
        }

        //校验新老用户标识
        Integer isNew = commonObj.getInteger("is_new");
        if (isNew == null) {
            return false;
        }
        //校验上报时间戳
        Long ts = jsonObject.getLong("ts");
        if (ts == null) {
            return false;
        }
        return true;
    }


}
